package com.zhuangjie.generator.common;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DirectoryUtils {
    /**
     * 获取临时目录路径
     *
     * @return {@link String}
     */
    public static String getTempDirectoryPath() {
        // 获取系统临时目录路径
        String os = System.getProperty("os.name").toLowerCase();

        if (os.contains("win")) {
            // Windows系统
            return System.getenv("TEMP");
        } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
            // Linux或Mac系统
            return "/tmp";
        } else {
            // 默认情况下，返回Java的默认临时目录路径
            return System.getProperty("java.io.tmpdir");
        }
    }
    public static String getTempDirectoryPathAndAppendModuleTime (String moduleName){
        // 获取当前的yyyy-MM-dd的字符串
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        return getTempDirectoryPath()+"/"+moduleName+"/"+formattedDate;
    }

    public static void openFolder(String directoryPath) {
        // 检查当前平台是否支持Desktop类
        if (Desktop.isDesktopSupported()) {
            Desktop desktop = Desktop.getDesktop();

            // 封装目录路径为File对象
            File directory = new File(directoryPath);

            // 检查文件或目录是否存在
            if (directory.exists()) {
                // 使用Desktop类打开目录
                try {
                    desktop.open(directory);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                System.out.println("目录不存在");
            }
        } else {
            System.out.println("不支持Desktop类");
        }

    }
}
